package main.common.exception;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import main.common.Bird;
import main.common.utils.RequestUtils;
import main.common.utils.ResponseEnum;
import org.junit.Test;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * gosn 综合应用
 * 
 * @author ChengGM
 * @create 2017-01-14 14:06
 **/
public class ServiceExceptionTest {
	@Test
	public void test() throws Exception {
		if (3 < 5) {
			throw new ServiceException(ResponseEnum.REQUEST_SUCESS, "成功");
		}
	}

	private final static Gson gson = new GsonBuilder()
			.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
			.create();

	@Test
	public void test2() throws Exception {
		List<Bird> list = new ArrayList<Bird>();
		Bird bird = new Bird();
		bird.setAge(1);
		bird.setName("张三");
		list.add(bird);

		Bird bird2 = new Bird();
		bird2.setAge(2);
		bird2.setName("李四");
		list.add(bird2);

		Bird bird3 = new Bird();
		bird3.setAge(3);
		bird3.setName("王五");
		list.add(bird3);

		Bird bird4 = new Bird();
		bird4.setAge(4);
		bird4.setName("赵六");
		list.add(bird4);

		Bird bird5 = new Bird();
		bird5.setAge(5);
		bird5.setName("田七");
		list.add(bird5);

		Type type = new TypeToken<List<Bird>>() {
		}.getType();
		System.out.println("原始-------->" + gson.toJson(list));
		List<Bird> result = new ArrayList<Bird>();
		result = (List<Bird>) RequestUtils.json2List(gson.toJson(list), type);
		System.out.println("转换后-------->" + gson.toJson(result));

	}
	@Test
	public void test3() throws Exception{
        Bird bird = new Bird();
        bird.setAge(1);
        bird.setName("张三");
        int a = 10;
        String[] list = {"a","b","c"};
        System.out.println(gson.toJson(bird));
        System.out.println(gson.toJson(list));
    }


}
